package org.smart4j.chapter1;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lyc on 2016-04-03.
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    /**
     * 1.继承HttpServlet，让它成为一个Servlet类
     * 2.覆盖父类doGet方法，用鱼接收GET请求
     * 3.在doGet方法中获取系统当前时间，并将其放入HttpServletRequest对象中，最后转发到/WEB-INF/jsp/hello.jsp页面
     * 4.使用WebServlet注解并配置请求路径，对外发布Servlet服务
     *
     * @param request             request请求
     * @param response            response响应
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(new Date());
        request.setAttribute("currentTime",currentTime);
        request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
    }

}
